package com.zhong.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhong.blog.mapper.CommentMapper;
import com.zhong.blog.pojo.Comment;
import com.zhong.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;


    @Override
    public int save(Comment comment) {

        int insert = commentMapper.insert(comment);
        return insert;
    }

    @Override
    public List<Comment> queryByblogId(int blogid) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blogid",blogid);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return comments;
    }

    @Override
    public List<Comment> getLatestComments() {
        Page<Comment> page = new Page<>(1, 5);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");

        commentMapper.selectPage(page, wrapper);
        List<Comment> records = page.getRecords();
        return records;
    }
}
